#!/bin/bash
# @@@ START COPYRIGHT @@@ 
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# @@@ END COPYRIGHT @@@

jpscmd=$JAVA_HOME/bin/jps
restserver_node=`hostname -f`

if [ -e $SQ_PDSH ]; then
   L_PDSH="$SQ_PDSH $MY_NODES "
else
   L_PDSH=
fi

restserver_port=`python << EOF
import os
from xml.dom import minidom

dcsconfig_dir = os.environ.get('REST_CONF_DIR')
if not dcsconfig_dir:
   name = os.environ.get('REST_INSTALL_DIR')
   restconfig_dir=name+"/conf"
doc = minidom.parse(restconfig_dir+"/rest-site.xml")
props = doc.getElementsByTagName("property")
for prop in props:
        pname = prop.getElementsByTagName("name")[0]
        if (pname.firstChild.data == "rest.port"):
           pvalue = prop.getElementsByTagName("value")[0]
           restPort=pvalue.firstChild.data
           print("%s" % (restPort))
EOF`

actual_restserver_cnt=`$L_PDSH $jpscmd |grep TrafodionRest |wc -l` 
if [[ ! -z $CLUSTERNAME ]]; then
   restserver_node=`$L_PDSH $jpscmd |grep TrafodionRest |cut -d":" -f1 |paste -sd ' ' -`
   restserver_pid=`$L_PDSH $jpscmd |grep TrafodionRest |cut -d" " -f2 |paste -sd ' ' -`
else
   restserver_pid=`$L_PDSH $jpscmd |grep TrafodionRest |cut -d" " -f1`
fi

echo
if [[ $actual_restserver_cnt > 0 ]]; then
   if [[ -z $restserver_port ]]; then
       restserver_port=4200
   fi
   echo "TrafodionRest listen port  : "$restserver_port
   echo "TrafodionRest is up on node: "$restserver_node",pid: "$restserver_pid
fi

echo -e "Process\t\tActual\t"
echo -e "---------\t------\t"
echo -e "TrafodionRest\t$actual_restserver_cnt\t"
echo

